import { ref } from 'vue'
import { getCategoryAPI } from '@/apis/category'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
export function useCategory() {
  // 数据
  const categoryData = ref({})

  // 获取一级分类
  const route = useRoute()
  const getCategory = async (id = route.params.id) => {
    const res = await getCategoryAPI(id)
    categoryData.value = res.result
  }
  getCategory()

  // 路由变化后重新请求数据
  onBeforeRouteUpdate((to) => {
    getCategory(to.params.id)
  })

  return {
    categoryData
  }
}